package net.luerything.restlet.gwt.rpc;

import org.restlet.Context;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.Restlet;
import org.restlet.data.Method;

/**
 * @author keke
 */
public abstract class GwtRpcRestlet extends Restlet {
  private GwtRpc gwtRpc;

  public GwtRpcRestlet(final GwtRpc gwtRpc) {
    this(null, gwtRpc);
  }

  public GwtRpcRestlet(final Context context, final GwtRpc gwtRpc) {
    super(context);
    assert gwtRpc != null;
    this.gwtRpc = gwtRpc;
  }

  @Override
  public void handle(final Request request, final Response response) {
    super.handle(request, response);
    if (request.getMethod().equals(Method.POST)) {
      Commons.Result result = gwtRpc.doRpc(this, request.getEntity());
      assert result != null;
      response.setEntity(result.getResult());
      response.setStatus(result.getStatus());
    } else {
      handleOther(request, response);
    }

  }

  protected void handleOther(final Request request, final Response response) {
    //do nothing.
  }
}
